// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   OptionScrPanel.java

package com.boci.clientapp.trading;

import com.boci.clientapp.main.ApplicationService;
import com.boci.clientapp.main.ClientAppLoader;
import com.boci.clientapp.trading.action.OptionAction;
import com.boci.clientapp.trading.vo.TradingVO;
import com.boci.common.constant.ClientAppConst;
import com.boci.common.ui.AbstractPanel;
import com.boci.common.ui.control.LimitDocument;
import com.boci.common.util.*;
import com.boci.common.vo.ResponseVO;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

public class OptionScrPanel extends AbstractPanel
{

    public OptionScrPanel(Locale pLocale, String pModuleName, String pScreenName)
    {
        super(pLocale, pModuleName, pScreenName);
        msgConf = ClientAppLoader.getMessagesConfigurator();
        hkOptFlag = true;
        initGUI();
        setText(pLocale);
    }

    public String getUnderlyingCodeSymbol()
    {
        String strUnderlyingCode = "";
        if(currentVO.getSymbol().equals(txtCodeSymbol.getText()) && currentVO.getUnderlyingSymbol() != null)
            strUnderlyingCode = currentVO.getUnderlyingSymbol();
        return strUnderlyingCode;
    }

    public void setText(Locale pLocale)
    {
        super.setText(pLocale);
        cmbOrderType.removeActionListener(this);
        setFont(pLocale);
        currentLocale = pLocale;
        btnShowBalance.setText(msgConf.getUIMessages("TRADING.SHOW_BALANCE", pLocale));
        if(ClientAppLoader.allowGetQuote())
            btnGetQuote.setText(msgConf.getUIMessages("TRADING.GETQUOTE", pLocale));
        btnHKOption.setText(msgConf.getUIMessages("TRADING.HK_OPTION", pLocale));
        btnUSOption.setText(msgConf.getUIMessages("TRADING.US_OPTION", pLocale));
        lblCodeSymbol.setText(msgConf.getUIMessages("TRADING.CODE", pLocale));
        lblQuantity.setText(msgConf.getUIMessages("TRADING.QUANTITY", pLocale));
        lblPrice.setText(msgConf.getUIMessages("TRADING.PRICE", pLocale));
        lblOrderType.setText(msgConf.getUIMessages("TRADING.ORDER_TYPE", pLocale));
        btnSell.setText(msgConf.getUIMessages("TRADING.SELL", pLocale));
        btnClear.setText(msgConf.getUIMessages("TRADING.CLEAR_ORDER", pLocale));
        String strOrdTypes[] = null;
        String strOrdTypeNames[] = null;
        if(hkOptFlag)
        {
            strOrdTypes = ClientAppLoader.getHKOptOrdTypes();
            lblTitle.setText(msgConf.getUIMessages("TRADING.HK_OPTION_TITLE", pLocale));
        } else
        if(!hkOptFlag)
        {
            strOrdTypes = ClientAppLoader.getUSOptOrdTypes();
            lblTitle.setText(msgConf.getUIMessages("TRADING.US_OPTION_TITLE", pLocale));
        }
        strOrdTypeNames = msgConf.getMsgArray("", strOrdTypes, pLocale);
        int intOldSelection = cmbOrderType.getSelectedIndex();
        cmbOrderType.setModel(new DefaultComboBoxModel(strOrdTypeNames));
        cmbOrderType.setSelectedIndex(intOldSelection);
        cmbOrderType.addActionListener(this);
    }

    private void setFont(Locale pLocale)
    {
        btnShowBalance.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
        if(ClientAppLoader.allowGetQuote())
            btnGetQuote.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
        lblTitle.setFont(ClientAppLoader.getLargeBoldTextFont(pLocale));
        lblCodeSymbol.setFont(ClientAppLoader.getLargeBoldTextFont(pLocale));
        lblQuantity.setFont(ClientAppLoader.getLargeBoldTextFont(pLocale));
        lblPrice.setFont(ClientAppLoader.getLargeBoldTextFont(pLocale));
        lblOrderType.setFont(ClientAppLoader.getLargeBoldTextFont(pLocale));
        btnSell.setFont(ClientAppLoader.getBoldButtonFont(pLocale));
        btnClear.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
        cmbOrderType.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
        if(hkOptFlag)
        {
            btnHKOption.setFont(ClientAppLoader.getBoldButtonFont(pLocale));
            btnUSOption.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
        } else
        {
            btnHKOption.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
            btnUSOption.setFont(ClientAppLoader.getBoldButtonFont(pLocale));
        }
    }

    public void initGUI()
    {
        setLayout(null);
        setBackground(Color.WHITE);
        lblHeader = new JLabel();
        lblHeader.setBounds(40, 40, 600, 30);
        add(lblHeader);
        btnShowBalance = new JButton();
        btnShowBalance.setBounds(600, 9, 120, 22);
        btnShowBalance.setActionCommand("SHOW BALANCE");
        btnShowBalance.addActionListener(this);
        add(btnShowBalance);
        btnHKOption = new JButton();
        btnHKOption.setBounds(20, 9, 120, 22);
        btnHKOption.setActionCommand("HK_OPTION");
        btnHKOption.addActionListener(this);
        add(btnHKOption);
        btnUSOption = new JButton();
        btnUSOption.setBounds(140, 9, 120, 22);
        btnUSOption.setActionCommand("US_OPTION");
        btnUSOption.addActionListener(this);
        add(btnUSOption);
        lblTitle = new JLabel();
        lblTitle.setBounds(40, 40, 600, 30);
        add(lblTitle);
        lblCodeSymbol = new JLabel();
        lblCodeSymbol.setHorizontalAlignment(4);
        lblCodeSymbol.setBounds(120, 68, 120, 20);
        add(lblCodeSymbol);
        lblQuantity = new JLabel();
        lblQuantity.setHorizontalAlignment(4);
        lblQuantity.setBounds(120, 93, 120, 20);
        add(lblQuantity);
        lblPrice = new JLabel();
        lblPrice.setHorizontalAlignment(4);
        lblPrice.setBounds(120, 118, 120, 20);
        add(lblPrice);
        lblOrderType = new JLabel();
        lblOrderType.setHorizontalAlignment(4);
        lblOrderType.setBounds(120, 143, 120, 20);
        add(lblOrderType);
        txtCodeSymbol = new JTextField();
        txtCodeSymbol.setBounds(255, 68, 200, 20);
        add(txtCodeSymbol);
        txtQuantity = new JTextField();
        txtQuantity.setBounds(255, 93, 200, 20);
        txtQuantity.setDocument(new LimitDocument(18));
        add(txtQuantity);
        txtPrice = new JTextField();
        txtPrice.setBounds(255, 118, 200, 20);
        txtPrice.setDocument(new LimitDocument(18));
        add(txtPrice);
        cmbOrderType = new JComboBox();
        cmbOrderType.setBounds(255, 143, 200, 20);
        cmbOrderType.addActionListener(this);
        add(cmbOrderType);
        btnGetQuote = new JButton();
        btnGetQuote.setBounds(465, 67, 120, 22);
        btnGetQuote.setActionCommand("GETQUOTE");
        btnGetQuote.addActionListener(this);
        if(ClientAppLoader.allowGetQuote())
            add(btnGetQuote);
        btnSell = new JButton();
        btnSell.setBounds(215, 173, 120, 25);
        btnSell.setActionCommand("SELL");
        btnSell.addActionListener(this);
        btnSell.setForeground(ClientAppConst.sellFontColor);
        add(btnSell);
        btnClear = new JButton();
        btnClear.setBounds(365, 173, 120, 25);
        btnClear.setActionCommand("CLEAR");
        btnClear.addActionListener(this);
        add(btnClear);
        borderPanel = new JPanel();
        borderPanel.setOpaque(false);
        borderPanel.setBorder(new EtchedBorder(1));
        borderPanel.setBounds(20, 38, 700, 165);
        add(borderPanel);
    }

    public boolean getHKOptFlag()
    {
        return hkOptFlag;
    }

    public void actionPerformed(ActionEvent e)
    {
        String strActionType = e.getActionCommand();
        if(e.getSource().equals(cmbOrderType))
            try
            {
                txtPrice.setEnabled(true);
                txtPrice.setBackground(Color.WHITE);
                String strNoPriceTypes[] = ClientAppLoader.getNoPriceOrdTypes();
                String strTypeNames[] = msgConf.getMsgArray("", strNoPriceTypes, currentLocale);
                String strSelectedType = (String)cmbOrderType.getSelectedItem();
                int i = 0;
                do
                {
                    if(i >= strTypeNames.length)
                        break;
                    if(strSelectedType.equals(strTypeNames[i]))
                    {
                        txtPrice.setText("");
                        txtPrice.setEnabled(false);
                        txtPrice.setBackground(Color.LIGHT_GRAY);
                        break;
                    }
                    i++;
                } while(true);
                return;
            }
            catch(Exception ex) { }
        if(validate(strActionType))
        {
            PerformanceLogger.startAppTimer();
            OptionAction optionAction = new OptionAction();
            optionAction.doProcess(getInputValue(strActionType), strActionType);
            PerformanceLogger.stopAppTimer();
            PerformanceLogger.doLog((new StringBuilder()).append(getScreenName()).append(".").append(strActionType).toString());
        }
    }

    public void refresh()
    {
        txtCodeSymbol.setText("");
        txtPrice.setText("");
        txtQuantity.setText("");
        int intDefIndex = 0;
        if(hkOptFlag)
            intDefIndex = ClientAppLoader.getDefaultOrdTypeIndex("HK_OPTION");
        else
            intDefIndex = ClientAppLoader.getDefaultOrdTypeIndex("US_OPTION");
        cmbOrderType.setSelectedIndex(intDefIndex);
        setDefFocus();
    }

    public void clearText()
    {
        txtCodeSymbol.setText("");
        txtPrice.setText("");
        txtQuantity.setText("");
        cmbOrderType.setSelectedIndex(0);
        setDefFocus();
    }

    public String getOptionQty()
    {
        return currentVO.getOptionQty();
    }

    public void refresh(ResponseVO pResponseVO)
    {
        if(pResponseVO instanceof TradingVO)
        {
            TradingVO tradingVO = (TradingVO)pResponseVO;
            currentVO = tradingVO;
            txtCodeSymbol.setText(tradingVO.getSymbol());
            String strPrice = StringUtil.formatPriceValue(tradingVO.getPrice());
            String strQty = StringUtil.formatPriceValue(tradingVO.getQuantity());
            txtPrice.setText(strPrice);
            txtQuantity.setText(strQty);
            String strOrdTypes[] = null;
            if(hkOptFlag)
                strOrdTypes = ClientAppLoader.getHKOptOrdTypes();
            else
                strOrdTypes = ClientAppLoader.getUSOptOrdTypes();
            for(int i = 0; i < strOrdTypes.length; i++)
                if(strOrdTypes[i].equals(tradingVO.getOrderType()))
                    cmbOrderType.setSelectedIndex(i);

            setDefFocus();
        }
    }

    public HashMap getInputValue(String pActionType)
    {
        HashMap inpMap = new HashMap();
        String strCode = txtCodeSymbol.getText();
        String strPrice = txtPrice.getText();
        String strQty = txtQuantity.getText();
        String strOrdTypeParam = "";
        String strOrdTypes[] = null;
        if(hkOptFlag)
            strOrdTypes = ClientAppLoader.getHKOptOrdTypes();
        else
            strOrdTypes = ClientAppLoader.getUSMktOrdTypes();
        strOrdTypeParam = "orderType";
        String strOrderType = strOrdTypes[cmbOrderType.getSelectedIndex()];
        inpMap.put(strOrdTypeParam, strOrderType);
        inpMap.put("symbol", strCode);
        inpMap.put("price2", strPrice);
        inpMap.put("qty", strQty);
        return inpMap;
    }

    public void chgOption(String pActionType)
    {
        Font hkOptFont = btnHKOption.getFont();
        Font usOptFont = btnUSOption.getFont();
        String strOrdTypes[] = null;
        String strOrdTypeNames[] = null;
        if(pActionType.equals("HK_OPTION"))
        {
            if(hkOptFlag)
                return;
            hkOptFlag = true;
            btnHKOption.setFont(hkOptFont.deriveFont(1));
            btnUSOption.setFont(usOptFont.deriveFont(0));
            lblTitle.setText(msgConf.getUIMessages("TRADING.HK_OPTION_TITLE", currentLocale));
            strOrdTypes = ClientAppLoader.getHKOptOrdTypes();
            lblTitle.setText(msgConf.getUIMessages("TRADING.HK_OPTION_TITLE", currentLocale));
        } else
        if(pActionType.equals("US_OPTION"))
        {
            if(!hkOptFlag)
                return;
            hkOptFlag = false;
            btnUSOption.setFont(usOptFont.deriveFont(1));
            btnHKOption.setFont(hkOptFont.deriveFont(0));
            lblTitle.setText(msgConf.getUIMessages("TRADING.US_OPTION_TITLE", currentLocale));
            strOrdTypes = ClientAppLoader.getUSOptOrdTypes();
            lblTitle.setText(msgConf.getUIMessages("TRADING.US_OPTION_TITLE", currentLocale));
        }
        strOrdTypeNames = msgConf.getMsgArray("", strOrdTypes, currentLocale);
        cmbOrderType.setModel(new DefaultComboBoxModel(strOrdTypeNames));
        refresh();
    }

    public boolean validate(String pActionType)
    {
        if(pActionType.equals("BUY") || pActionType.equals("SELL"))
        {
            ArrayList msgKeyList = new ArrayList();
            boolean validFlag = true;
            String strCode = txtCodeSymbol.getText();
            String strPrice = txtPrice.getText();
            String strQty = txtQuantity.getText();
            String strOrderType = (String)cmbOrderType.getSelectedItem();
            if(strCode == null || strCode.length() == 0)
            {
                msgKeyList.add("TRADING.MISSING_CODE");
                validFlag = false;
            }
            if(strQty == null || strQty.length() == 0)
            {
                msgKeyList.add("TRADING.MISSING_QTY");
                validFlag = false;
            } else
            {
                try
                {
                    Long.valueOf(strQty);
                }
                catch(NumberFormatException e)
                {
                    msgKeyList.add("TRADING.QTY_NOT_NUMERIC");
                    validFlag = false;
                }
            }
            if(txtPrice.isEnabled())
            {
                if(strPrice != null)
                    strPrice = strPrice.trim();
                if(strPrice == null || strPrice.length() == 0)
                {
                    msgKeyList.add("TRADING.MISSING_PRICE");
                    validFlag = false;
                } else
                {
                    try
                    {
                        Double.valueOf(strPrice);
                        int intDecimalPlace = strPrice.indexOf(".");
                        int intNumOfDec = strPrice.length() - intDecimalPlace - 1;
                        if(intDecimalPlace > 0)
                            if(getHKOptFlag())
                            {
                                if(intNumOfDec > ClientAppLoader.getHKDecimalLimit())
                                {
                                    msgKeyList.add("TRADING.INVALID_PRICE");
                                    validFlag = false;
                                }
                            } else
                            if(intNumOfDec > ClientAppLoader.getUSDecimalLimit())
                            {
                                msgKeyList.add("TRADING.INVALID_PRICE");
                                validFlag = false;
                            }
                    }
                    catch(NumberFormatException e)
                    {
                        msgKeyList.add("TRADING.PRICE_NOT_NUMERIC");
                        validFlag = false;
                    }
                }
            }
            if(strOrderType == null || strOrderType.length() == 0)
            {
                msgKeyList.add("TRADING.MISSING_ORDERTYPE");
                validFlag = false;
            }
            if(!validFlag)
            {
                ApplicationService.getInstance().showWarningDialog(null, msgKeyList);
                return false;
            }
        }
        return true;
    }

    public void setDefFocus()
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                if(txtCodeSymbol.getText() == null || txtCodeSymbol.getText().length() == 0)
                    txtCodeSymbol.requestFocus();
                else
                if(txtQuantity.getText() == null || txtQuantity.getText().length() == 0)
                    txtQuantity.requestFocus();
                else
                if(txtPrice.getText() == null || txtPrice.getText().length() == 0)
                    txtPrice.requestFocus();
                else
                    txtCodeSymbol.requestFocus();
            }
        }
);
    }

    private JLabel lblTitle;
    private JLabel lblHeader;
    private JButton btnShowBalance;
    private JButton btnHKOption;
    private JButton btnUSOption;
    private JLabel lblCodeSymbol;
    private JLabel lblQuantity;
    private JLabel lblPrice;
    private JLabel lblOrderType;
    private JTextField txtCodeSymbol;
    private JTextField txtQuantity;
    private JTextField txtPrice;
    private JComboBox cmbOrderType;
    private JButton btnGetQuote;
    private JButton btnSell;
    private JButton btnClear;
    private JPanel borderPanel;
    private MessagesConfigurator msgConf;
    private Locale currentLocale;
    private boolean hkOptFlag;
    private TradingVO currentVO;



}
